require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'select2': '/js/lib/select2.min',
        'bootstrap': '/js/lib/bootstrap.min'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

var myFun = {};

require(['jquery', 'doT', 'common', 'bootstrap'], function($, doT, common) {
    $(function() {
        $('.sidebar ul li').each(function() {
            if ($(this).text() == '食堂管理') {
                $(this).parent().css('display', 'block');
                $(this).children().css('color', '#46A3FF');
            }
        });

        function getId() {
            var p = location.search.split('?')[1];
            var items = p.split('&');
            myFun.item = {};
            $.each(items, function() {
                var key = this.split('=')[0];
                var value = this.split('=')[1];
                myFun.item[key] = value;
            });
        }
        getId();


        function initCanteen() {
            $.get(common.urlBase + '/shop/viewShop/' + myFun.item['id'] + '.json', function(data, statu) {
                data = eval('(' + data + ')');
                if (data['ok']) {
                    data = data.data;
                    myFun.school_id = data['school_id'];
                    for (var d in data) {
                        if (d != 'shop_logo') {
                            $('#' + d).val(data[d]);
                        }
                    }
                    myFun.shop_logo_old = data['shop_logo'];
                    $('#preview').attr('src', 'http://pushi2016.com/' + myFun.shop_logo_old);
                    $('#preview').css('display', 'block');
                    $('#preview').show();
                } else {
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                    return false;
                }
            });
        }
        initCanteen();

        $('#change_canteen').submit(function() {
            var imgurl_old = 'http://pushi2016.com/' + myFun.shop_logo_old;
            (imgurl_old == $('#preview').attr('src')) ? sendData(myFun.shop_logo_old) : sendImg();

            return false;
        });

        $('#fileu').on('change', function() {
            var reader = new FileReader();
            reader.onload = function(e) {
                $('#preview').attr('src', e.target.result);
                if (/image/.test($('#fileu')[0].files[0].type)) {
                    $('#preview').css('display', 'block');
                    $('#preview').show();
                } else {
                    $('#errorMessage').show();
                }
            };
            reader.readAsDataURL($('#fileu')[0].files[0]);
        });
    });

    function sendImg() {
        var formData = new FormData();
        formData.append('fileu', $('#fileu')[0].files[0]);
        $.ajax({
            url: common.urlBase + '/file/fileUpload.json',
            data: formData,
            type: 'POST',
            processData: false,
            contentType: false,
            success: function(response) {
                var data = eval('(' + response + ')');
                if (data['ok']) {
                    shop_logo = data.data.file;
                    sendData(shop_logo);
                } else {
                    $('#noticeMsg').text('上传图片失败！');
                    $('#noticeModal').modal('show');
                    return false;
                }

            }
        });
    }

    function sendData(shop_logo) {
        var name = $('#shop_name').val();
        var shop_url = $('#shop_url').val();
        var youzan_group_id = $('#youzan_group_id').val();
        var master_id = $('#master_id').val();

        var d = {
            'shop_id': myFun.item['id'],
            'shop_name': name,
            'shop_logo': shop_logo,
            'shop_url': shop_url,
            'school_id': myFun.school_id,
            'youzan_group_id': youzan_group_id,
            'master_id': master_id
        };

        $.post(common.urlBase + '/shop/editShop/edit.json', d, function(data, statu) {
            var data = eval('(' + data + ')');
            if (data['ok']) {
                $('#noticeMsg').text('修改成功！');
                $('#noticeModal').modal('show');
                window.location = '/views/canteen_manage.html';
            } else {
                $('#noticeMsg').text(data['message']);
                $('#noticeModal').modal('show');
            }
        });
    }


});
